local game = require "club.threecard.texas"
local consts = game.util.consts

local M = game.ui.element()

M.store_watchers = {
    ingame = {
        ["$"] = function(self, store, state)
            self:hide()
        end,

        ["请求玩家操作"] = function(self, store, state, args)
            if state.current_no ~= game.kbe.player().seat.no then
                self:hide()
                return
            end
            self:show()
        end,

        ["玩家操作"] = function(self, store, state, args)
            self:hide()
        end
    }
}

function M:ctor()
    self.res = "club.threecard.texas/ui/ingame.b:operator"
    self.operations = {}
end

function M:on_load()
    self:super("on_load")    
    self.operations[consts.flop.id] = self.btn_flop
    self.operations[consts.check.id] = self.btn_check
    self.operations[consts.call.id] = self.btn_call
    self.operations[consts.raise_half_pot.id] = self.btn_raise_half_pot
    self.operations[consts.raise_pot.id] = self.btn_riase_pot
    self.operations[consts.allin.id] = self.btn_allin
end

function M:on_injected(bind)
    bind("button.click", self.btn_flop, self.flop_ivk)
    bind("button.click", self.btn_check, self.check_ivk)
    bind("button.click", self.btn_call, self.call_ivk)
    bind("button.click", self.btn_raise_half_pot, self.raise_half_pot_ivk)
    bind("button.click", self.btn_raise_pot, self.raise_pot_ivk)
    bind("button.click", self.btn_allin, self.allin_ivk)
end

function M:show()
    self:super("show")
    local state = game.store.ingame.state
    local operation_ids = state.current_operation_ids
    local operation_value = state.current_operation_values

    for _, btn in pairs(self.operations) do
        btn.gameObject:SetActive(false)
    end

    for _, id in ipairs(operation_ids) do
        if self.operations[id] then
            self.operations[id].gameObject:SetActive(true)
        end
    end
end

function M:flop_ivk()
    game.kbe.player():operate(consts.flop.id)
    self:hide()
end

function M:call_ivk()
    game.kbe.player():operate(consts.call.id)
    self:hide()
end

function M:check_ivk()
    game.kbe.player():operate(consts.check.id)
    self:hide()
end

function M:raise_half_pot_ivk()
    game.kbe.player():operate(consts.raise_half_pot.id)
    self:hide()
end

function M:raise_pot_ivk()
    game.kbe.player():operate(consts.raise_pot.id)
    self:hide()
end

function M:allin_ivk()
    game.kbe.player():operate(consts.allin.id)
    self:hide()
end

return M